// main.cpp
// UNICODE 版本，MinGW-w64 直接通过
// 编译命令：
//   g++ main.cpp -o demo.exe -mwindows -static -s -DUNICODE -D_UNICODE

#include <windows.h>

// 全局实例句柄
HINSTANCE g_hInst;

// 窗口类名（宽字符）
const wchar_t CLASS_NAME[] = L"MyUnicodeWindowClass";

// 窗口过程函数前置声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

// 程序入口：UNICODE 版 wWinMain
int WINAPI wWinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    PWSTR     lpCmdLine,
    int       nCmdShow)
{
    g_hInst = hInstance;

    // 1. 设计窗口类（WNDCLASSEXW）
    WNDCLASSEXW wc = { 0 };
    wc.cbSize        = sizeof(wc);
    wc.lpfnWndProc   = WndProc;                   // 窗口过程
    wc.hInstance     = hInstance;
    wc.hCursor       = LoadCursorW(nullptr, IDC_ARROW);
    wc.lpszClassName = CLASS_NAME;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

    // 2. 注册窗口类
    if (!RegisterClassExW(&wc))
        return 0;

    // 3. 创建主窗口（CreateWindowExW）
    HWND hwnd = CreateWindowExW(
        0,
        CLASS_NAME,
        L"Win32 UNICODE Demo (MinGW-w64)",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        nullptr, nullptr, hInstance, nullptr);

    if (!hwnd)
        return 0;

    // 4. 显示并更新
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // 5. 消息循环
    MSG msg;
    while (GetMessageW(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    return (int)msg.wParam;
}

// 窗口过程
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CREATE:
        // 创建按钮（CreateWindowW）
        CreateWindowW(
            L"BUTTON",
            L"点我",
            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            50, 50, 120, 40,
            hwnd,
            (HMENU)1,      // 控件 ID
            g_hInst,
            nullptr);
        return 0;

    case WM_COMMAND:
        if (LOWORD(wParam) == 1)
        {
            // 弹消息框（MessageBoxW）
            MessageBoxW(hwnd,
                        L"Hello, Windows UNICODE!",
                        L"尼玛",
                        MB_OK | MB_ICONINFORMATION);
        }
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProcW(hwnd, msg, wParam, lParam);
}